#include "changeusercoverdialog.h"
#include "changeusercovertask.h"
#include "libs/controls/toastlabel.h"
#include "libs/core/user/localuser.h"
#include "libs/thirdparty/imageutils/imageutils.h"
#include "usersettings.h"
#include "utils/utils.h"
#include "yingce.h"
#include <QFileDialog>
#include <QFileInfo>
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>

ChangeCoverDialog::ChangeCoverDialog(QWidget* parent)
    : QDialog { parent, Qt::MSWindowsFixedSizeDialogHint }
{
    setWindowTitle(tr("Change Cover"));
    setMinimumSize(450, 520);
    resize(450, 520);

    QVBoxLayout* vl = new QVBoxLayout(this);
    vl->setContentsMargins(0, 0, 0, 20);
    vl->setSpacing(20);

    cover_label_ = new QLabel(this);
    cover_label_->setObjectName("cover");
    cover_label_->setScaledContents(true);
    vl->addWidget(cover_label_, 1);

    QPushButton* change_button = new QPushButton(tr("Change"), this);
    change_button->setObjectName("change");
    vl->addWidget(change_button, 0, Qt::AlignHCenter);

    connect(change_button, &QPushButton::clicked, this, [this] {
        QString path = QFileDialog::getOpenFileName(
                this, tr("Select Cover"), UserSettings::instance().accessDir().absolutePath(),
                tr("Images (*.png *.jpg *.jpeg)"));
        if (!path.isEmpty()) {
            QFileInfo fi(path);
            if (fi.size() > 2 * 1024 * 1024) {
                utils::msgBox(this, tr("Select Cover"),
                              tr("The image file size is greater than 2MB, please select a file no "
                                 "larger than 2MB."));
                return;
            }
            if (fi.size() > 500 * 1024) {
                if (!imageutils::compressImage(path, UserSettings::instance().tempDir(), path)) {
                    yc::ToastLabel::showMessage(
                            tr("Failed to compress the image, pls choose another."), this);
                    return;
                }
            }
            QPixmap pixmap(path);
            if (pixmap.width() == 0) {
                yc::ToastLabel::showMessage(tr("Failed to load the image, pls choose another."),
                                            this);
                return;
            }
            const QSize sz = cover_label_->size();
            cover_label_->setPixmap(pixmap.width() > sz.width() || pixmap.height() > sz.height()
                                            ? pixmap.scaled(sz)
                                            : pixmap);
            ChangeUserCoverTask* t = new ChangeUserCoverTask(path, this);
            connect(t, &ChangeUserCoverTask::finished, this,
                    [this] { yc::ToastLabel::showMessage(tr("Success."), this); });
            connect(t, &ChangeUserCoverTask::failed, this, [this](int error, const QString& msg) {
                yc::ToastLabel::showMessage(msg, this);
            });
            t->start();
        }
    });
}

void ChangeCoverDialog::resizeEvent(QResizeEvent* event)
{
    QDialog::resizeEvent(event);
    if (!YingCe::instance()->localUser()->cover().isEmpty()) {
        const QString path = UserSettings::instance().resDir().absoluteFilePath(
                YingCe::instance()->localUser()->cover());
        cover_label_->setPixmap(utils::uniformPixmap(path, cover_label_->size()));
    }
}
